/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package peticionweb.control;

import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import peticionweb.logica.entidad.Cupar1a;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import peticionweb.logica.entidad.Cuyea1m;
import peticionweb.control.exceptions.IllegalOrphanException;
import peticionweb.control.exceptions.NonexistentEntityException;
import peticionweb.control.exceptions.PreexistingEntityException;

/**
 *
 * @author icardenas
 */
public class Cuyea1mJpaController implements Serializable {

    public Cuyea1mJpaController() {
        EntityManagerFactory factory = Persistence.createEntityManagerFactory("PeticionWebPU");
        this.emf = factory;
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Cuyea1m cuyea1m) throws PreexistingEntityException, Exception {
        if (cuyea1m.getCupar1aList() == null) {
            cuyea1m.setCupar1aList(new ArrayList<Cupar1a>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            List<Cupar1a> attachedCupar1aList = new ArrayList<Cupar1a>();
            for (Cupar1a cupar1aListCupar1aToAttach : cuyea1m.getCupar1aList()) {
                cupar1aListCupar1aToAttach = em.getReference(cupar1aListCupar1aToAttach.getClass(), cupar1aListCupar1aToAttach.getCupar1aPK());
                attachedCupar1aList.add(cupar1aListCupar1aToAttach);
            }
            cuyea1m.setCupar1aList(attachedCupar1aList);
            em.persist(cuyea1m);
            for (Cupar1a cupar1aListCupar1a : cuyea1m.getCupar1aList()) {
                Cuyea1m oldCuyea1mOfCupar1aListCupar1a = cupar1aListCupar1a.getCuyea1m();
                cupar1aListCupar1a.setCuyea1m(cuyea1m);
                cupar1aListCupar1a = em.merge(cupar1aListCupar1a);
                if (oldCuyea1mOfCupar1aListCupar1a != null) {
                    oldCuyea1mOfCupar1aListCupar1a.getCupar1aList().remove(cupar1aListCupar1a);
                    oldCuyea1mOfCupar1aListCupar1a = em.merge(oldCuyea1mOfCupar1aListCupar1a);
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findCuyea1m(cuyea1m.getIdeye()) != null) {
                throw new PreexistingEntityException("Cuyea1m " + cuyea1m + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Cuyea1m cuyea1m) throws IllegalOrphanException, NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Cuyea1m persistentCuyea1m = em.find(Cuyea1m.class, cuyea1m.getIdeye());
            List<Cupar1a> cupar1aListOld = persistentCuyea1m.getCupar1aList();
            List<Cupar1a> cupar1aListNew = cuyea1m.getCupar1aList();
            List<String> illegalOrphanMessages = null;
            for (Cupar1a cupar1aListOldCupar1a : cupar1aListOld) {
                if (!cupar1aListNew.contains(cupar1aListOldCupar1a)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Cupar1a " + cupar1aListOldCupar1a + " since its cuyea1m field is not nullable.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            List<Cupar1a> attachedCupar1aListNew = new ArrayList<Cupar1a>();
            for (Cupar1a cupar1aListNewCupar1aToAttach : cupar1aListNew) {
                cupar1aListNewCupar1aToAttach = em.getReference(cupar1aListNewCupar1aToAttach.getClass(), cupar1aListNewCupar1aToAttach.getCupar1aPK());
                attachedCupar1aListNew.add(cupar1aListNewCupar1aToAttach);
            }
            cupar1aListNew = attachedCupar1aListNew;
            cuyea1m.setCupar1aList(cupar1aListNew);
            cuyea1m = em.merge(cuyea1m);
            for (Cupar1a cupar1aListNewCupar1a : cupar1aListNew) {
                if (!cupar1aListOld.contains(cupar1aListNewCupar1a)) {
                    Cuyea1m oldCuyea1mOfCupar1aListNewCupar1a = cupar1aListNewCupar1a.getCuyea1m();
                    cupar1aListNewCupar1a.setCuyea1m(cuyea1m);
                    cupar1aListNewCupar1a = em.merge(cupar1aListNewCupar1a);
                    if (oldCuyea1mOfCupar1aListNewCupar1a != null && !oldCuyea1mOfCupar1aListNewCupar1a.equals(cuyea1m)) {
                        oldCuyea1mOfCupar1aListNewCupar1a.getCupar1aList().remove(cupar1aListNewCupar1a);
                        oldCuyea1mOfCupar1aListNewCupar1a = em.merge(oldCuyea1mOfCupar1aListNewCupar1a);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                String id = cuyea1m.getIdeye();
                if (findCuyea1m(id) == null) {
                    throw new NonexistentEntityException("The cuyea1m with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(String id) throws IllegalOrphanException, NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Cuyea1m cuyea1m;
            try {
                cuyea1m = em.getReference(Cuyea1m.class, id);
                cuyea1m.getIdeye();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The cuyea1m with id " + id + " no longer exists.", enfe);
            }
            List<String> illegalOrphanMessages = null;
            List<Cupar1a> cupar1aListOrphanCheck = cuyea1m.getCupar1aList();
            for (Cupar1a cupar1aListOrphanCheckCupar1a : cupar1aListOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Cuyea1m (" + cuyea1m + ") cannot be destroyed since the Cupar1a " + cupar1aListOrphanCheckCupar1a + " in its cupar1aList field has a non-nullable cuyea1m field.");
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            em.remove(cuyea1m);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Cuyea1m> findCuyea1mEntities() {
        return findCuyea1mEntities(true, -1, -1);
    }

    public List<Cuyea1m> findCuyea1mEntities(int maxResults, int firstResult) {
        return findCuyea1mEntities(false, maxResults, firstResult);
    }

    private List<Cuyea1m> findCuyea1mEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Cuyea1m.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Cuyea1m findCuyea1m(String id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Cuyea1m.class, id);
        } finally {
            em.close();
        }
    }

    public int getCuyea1mCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Cuyea1m> rt = cq.from(Cuyea1m.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
